<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
  // 栈
class Stack{
   constructor(){
     this.item = []
   }
   push(element){
    this.item.push(element)
   }
   pop(element){
     return this.item.pop()
   }
   isEmpty(){
     return this.item.length === 0
   }
   size(){
     return this.item.length
   }
   peek(){
     return this.item[this.item.length-1]
   }
}
  //队列
class Queue{
  constructor(){
    this.item = []
  }
  enqueue(element){
    this.item.push(element)
  }
  denqueue(element){
    return this.item.shift(element)
  }
  front(){
    return this.item[0]
  }
  isEmpty(){
   return this.item.length === 0
  }
  size(){
    return this.item.length
  }
}
  //链表
function LinkedList(){
  function Node(element){
    this.element = element
    this.next = null
  }
  this.head = null
  this.length = 0

  LinkedList.prototype.append = function (){
    let newNode = new Node(element)
    if( this.head = null ){ this.head = newNode } 
    let current = this.head     
      while(current.next){
         current = current.next
      }
      current.next = newNode
      this.length++
 }
  LinkedList.prototype.removeAt = function(position){
      if(position < 0 || position >= this.length)  return false
      let current = this.head
      let previous = null
      for( let i = 0;i < position; i++ ){
          previous = current
          current = current.next
      }
        previous.next = current.next
       this.length--
  }
  LinkedList.prototype.insert = function(position,element){
    if(position < 0 || position >= this.length)  return false
      let newNode = new Node(element)
      let current = this.head
      let previous = null
     if( position == 0 ){
      this.head = newNode
      newNode.next = current
    }
     for(let i = 0; i < position; i++ ){
       previous = current
       current = current.next
     }
     current.next = newNode
     this.length++
  }
  LinkedList.prototype.indexOf = function(element){
     let current = this.head
     let index = 0
     while(current){
       if( element = this.element ){
         return index
       }
       index++
       current = current.next
     }
  }
  LinkedList.prototype.remove = function(element){
        let index  =  this.indexOf(element)
        this.removeAt(index)
  }
  LinkedList.prototype.isEmpty = function(){
     return this.length === 0
  }
  LinkedList.prototype.size = function(){
       return this.length
  }
}
//集合
class Set {
  constructor(){
    this.item = {}
  }
  has(value){
    this.item.hasOwnProperty(value)
  }
 add(value){
   if(this.item.has(value)) return false
    this.item[value] = value
    return true
 }
 remove(value){
  if(this.item.has(value)) return false
    delete this.item[value]
    return true
 }

 clear(){
   this.item = {}
 }
 size(){

   let count = 0
   for (var k in this.item){
     if(this.item.has(k))
     count ++
   }
   return count
  //  Object.keys(this.item).length
 }
 values(){
   let keys = []
   for(var k in this.item){
     keys.push(this.item[k])
   }
  //  Object.keys(this.item)
 }
}
//二叉树
function BinarySearchTree(){
     function node(key){
         this.key = key
         this.left = null
         this.right = null
     }
     this.root = null

 BinarySearchTree.prototype.insert(key) = function(){
      let newNode = new node(key)
      if(this.root == null){
        this.root = newNode
      }else{
        this.insertNode(this.root,newNode)
      }
 }
 BinarySearchTree.prototype.insertNode = function(node,newNode){
   if( newNode.key < node.key){
     if( node.left == null){
      node.left = newNode
     }else{
      this.insertNode(node.left,newNode)
     }
   }else{
    if( node.right == null){
       node.right = newNode
     }else{
       this.insertNode(node.right,newNode)
     }
   }
 }
 BinarySearchTree.prototype.max = function(){
   let current = this.root
   while(current){
     current = current.left
   }
   return current.key
 }
 BinarySearchTree.prototype.min = function(){
    let current = this.root
    while(current){
      current = current.right
    }
    return current.key
 }

}




  </script>
</body>
</html>